﻿package dut.ir.lxb.weibo.spider;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import dut.ir.lxb.weibo.bean.EventBean;
import dut.ir.lxb.weibo.bean.UserWeiboAction;
import dut.ir.lxb.weibo.bean.WeiboBean;
import dut.ir.lxb.weibo.db.EventInfoDao;
import dut.ir.lxb.weibo.db.UserInfoDao;
import dut.ir.lxb.weibo.db.UserWeiboActionDao;
import dut.ir.lxb.weibo.db.WeiboInfoDao;
import dut.ir.lxb.weibo.main.EventDownloadTest;
import dut.ir.lxb.weibo.util.ActionType;
import dut.ir.lxb.weibo.util.SpiderConfig;

public class EventDownloadThread extends DownloadThread
{
	
	protected static Logger logger = EventDownloadTest.logger;
	
	/*
	static
	{
		PropertyConfigurator.configure("./config/log4j.properties");
		DailyRollingFileAppender appender = (DailyRollingFileAppender)Logger.getRootLogger().getAppender("DayRollingFile");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateValue = sdf.format(new Date());
		appender.setFile(String.format("./log/%s.log", dateValue));
		appender.activateOptions();
	}
	*/
	
	public List<EventBean> initEventList()
	{
		
		List<EventBean> eventList = new ArrayList<EventBean>();
		
		/*
		EventBean eb = new EventBean();
		eb.setUrl("http://weibo.com/p/100808ce7f80d0fec082a9db24746c8e11e64c");
		eb.setEventCode("100808ce7f80d0fec082a9db24746c8e11e64c");
		eb.setEventname("我的双11");
		eb.setHostPerson("天猫");
		eb.setType("社会");
		eb.setDescription("#我的双11#来啦！这个双11你打算怎么过？");
		eventList.add(eb);
		
		eb = new EventBean();
		eb.setUrl("http://weibo.com/p/1008089fc633e5562bf352698e0fdd0b582f7c?from=faxian_huati");
		eb.setEventCode("1008089fc633e5562bf352698e0fdd0b582f7c");
		eb.setEventname("双十一");
		eb.setHostPerson("天猫");
		eb.setType("社会");
		eb.setDescription("这个双十一，你打算买些什么呢？");
		eventList.add(eb);
		*/
		
		EventInfoDao eventDao = EventInfoDao.getInstance();
		EventBean eb = eventDao.getEventByName("APEC");
		//eventList.add(eb);
		
		eb = new EventBean();
		eb.setUrl("http://weibo.com/p/10080879dba347b4719fd1dc48dabf950f9414?from=faxian_huati");
		eb.setEventCode("10080879dba347b4719fd1dc48dabf950f9414");
		eb.setEventname("崔永元清华教授骂战");
		eb.setHostPerson("新浪娱乐");
		eb.setType("明星");
		eb.setDescription("近日，清华教授肖鹰批评赵本山的二人转是低俗秀，因而与崔永元在微博上掀起了一场骂战，而且至今仍在继续，骂战不断升级。");
		eventList.add(eb);
		
		eb = new EventBean();
		eb.setUrl("http://weibo.com/p/10080818816ab68986b500e6e667235048b803?from=faxian_huati");
		eb.setEventCode("10080818816ab68986b500e6e667235048b803");
		eb.setEventname("学生挨批泼老师热水");
		eb.setHostPerson("新京报");
		eb.setType("社会");
		eb.setDescription("华东政法大学松江校区一老师在第一节课时点名，一女生因复习考研，直到第二节课才到，老师对其进行了批评教育。课间休息时女生主动拿老师杯子去接热水，“老师还说谢谢。”不料女生端起杯子，直接泼向老师面部。据悉，该校饮水机热水常年保持98摄氏度。");
		eventList.add(eb);
		
		
		eb = new EventBean();
		eb.setUrl("http://weibo.com/p/100808287289b8bcf029bae6c7482022c719fe?from=faxian_huati");
		eb.setEventCode("100808287289b8bcf029bae6c7482022c719fe");
		eb.setEventname("吴佩慈退出演艺圈");
		eb.setHostPerson("新浪娱乐");
		eb.setType("明星");
		eb.setDescription("13日，吴佩慈受邀出席名牌鞋款活动，产后首度公开亮相。乐于当妈的她竟然在活动上宣布：“将全面退出演艺圈！”");
		eventList.add(eb);
		
		eb = new EventBean();
		eb.setUrl("http://weibo.com/p/100808091a3058cd05a5d3ef88a3c95eb6508f");
		eb.setEventCode("100808091a3058cd05a5d3ef88a3c95eb6508f");
		eb.setEventname("王菲谢霆锋复合");
		eb.setHostPerson("俞健勇菲比寻常");
		eb.setType("明星");
		eb.setDescription("9月15日，有记者拍到到谢霆锋竟和王菲一起坐在车上画面，随后四天更拍摄王菲谢霆锋公寓内缠绵厮守，上演“深情之吻”。谢霆锋经纪人霍汶希在20日中午更新微博，默认复合属实，并指责狗仔偷拍行为。9月24李亚鹏发微博祝福“孩儿她娘”。");
		eventList.add(eb);
		
		
		
		eb = new EventBean();
		eb.setUrl("http://weibo.com/p/100808d06a57fbb90e16fa04c3a2c0cf6515e7?from=faxian_huati");
		eb.setEventCode("100808d06a57fbb90e16fa04c3a2c0cf6515e7");
		eb.setEventname("撑同志反歧视");
		eb.setHostPerson("小雀女");
		eb.setType("社会");
		eb.setDescription("每个人都有爱的权利，爱情无关性别！！！撑同志，反歧视！");
		eventList.add(eb);
		
		
		return eventList;
	}
	
	
	
	
	@Override
	public void run()
	{
		
		WeiboDownloader wd = downloaderList.poll();
		wd.init();
		wd.setAccessDataBase(true);
		if(!wd.checkAccount())
			wd = UserWeiboDownloadThread.getUsefulDownloader(downloaderList);
		
		//while(true)
		//{
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateValue = df.format(new Date());
			
			List<EventBean> eventList;//initEventList();
			
			
			
			eventList = (wd.downloadEventList(SpiderConfig.getMaxEventPageCount()));
			eventList = eventList.subList(0, SpiderConfig.getMaxEventCount());
			//eventList.addAll(initEventList());
			
			UserInfoDao userDao = UserInfoDao.getInstance();
			EventInfoDao eventDao = EventInfoDao.getInstance();
			WeiboInfoDao weiboDao = WeiboInfoDao.getInstance();
			UserWeiboActionDao uwaDao = UserWeiboActionDao.getInstance();
			
			/*test
			//将此微博的个人信息插入数据库中
			try
			{
				userDao.insertUser(wd.downloadUserInfo("3968323533"));
			} catch (AccountForbiddonException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			*/
			
			
			int startIndex = SpiderConfig.getStratEventIndex();
			//逐个下载事件
			for(int i = startIndex; i < eventList.size(); i++)
			{
				EventBean currentEvent = eventList.get(i);
				
				try
				{
					dateValue = df.format(new Date());
					String eventInfo = String.format("[%s]正在下载事件[%s]", dateValue, currentEvent.getEventname());
					logger.info(eventInfo);
					//System.out.println(eventInfo);
					//将此事件插入数据库中
					eventDao.insertEventInfo(currentEvent);
					//先下载事件原始微博
					List<WeiboBean> hotWeiboList = wd.downloadEventHotWeiboList(currentEvent);
					List<WeiboBean> weiboList = wd.downloadEventWeiboList(currentEvent);
					
					System.out.println(String.format("已下载事件[%s]的[%d]条微博，其中[%d]条热门微博", currentEvent.getEventname(), weiboList.size() + hotWeiboList.size(), hotWeiboList.size()));
					
					if(hotWeiboList != null)
					{
						for(WeiboBean weibo : hotWeiboList)
						{
							if(weibo.getForwardNum() == 0) continue;
							
							dateValue = df.format(new Date());
							
							int forwardCount = weibo.getForwardNum();
							int forwardCountDb = uwaDao.getForwardCountByMid(weibo.getMid());
							if(forwardCountDb / forwardCount > SpiderConfig.getForwardCountThreshold())
							{
								String info = String.format("[%s] 微博[%s]的转发信息已下载，跳过", dateValue, weibo.getMid());
								System.out.println(info);
								continue;
							}
							
							String forwardInfo = String.format("[%s] 正在下载事件 [%s] 微博 [%s] 的转发信息", dateValue, currentEvent.getEventname(), weibo.getMid());
							logger.info(forwardInfo);
							
							//下载此微博的转发信息
							wd.downloadUserWeiboAction(weibo, ActionType.FORWARD);
					
						}
					}
					
					//目前先下载微博，转发等其它信息后面再进行下载
					if(true)
						continue;
					
					//先将参与此事件的个人信息进行下载
					for(WeiboBean weibo : hotWeiboList)
					{
						//将此微博的个人信息插入数据库中
						//userDao.insertUser(wd.downloadUserInfo(weibo.getUid()));
						dateValue = df.format(new Date());
						String forwardInfo = String.format("[%s] 正在下载事件 [%s] 的用户 [%s] 的个人信息", dateValue, currentEvent.getEventname(), weibo.getUid());
						logger.info(forwardInfo);
						weiboDao.insertWeibo(weibo);
					}
					for(WeiboBean weibo : weiboList)
					{
						//将此微博的个人信息插入数据库中
						//userDao.insertUser(wd.downloadUserInfo(weibo.getUid()));
						dateValue = df.format(new Date());
						String forwardInfo = String.format("[%s] 正在下载事件 [%s] 的用户 [%s] 的个人信息", dateValue, currentEvent.getEventname(), weibo.getUid());
						logger.info(forwardInfo);
						weiboDao.insertWeibo(weibo);
					}
					
					
					
					
					for(WeiboBean weibo : weiboList)
					{
						//将此微博的个人信息插入数据库中
						//userDao.insertUser(wd.downloadUserInfo(weibo.getUid()));
						//少于最小转发数，则路过之
						if(weibo.getForwardNum() < SpiderConfig.getMinForwardNumber())
							continue;
						dateValue = df.format(new Date());
						String forwardInfo = String.format("[%s] 正在下载事件 [%s] 微博 [%s] 的转发信息", dateValue, currentEvent.getEventname(), weibo.getMid());
						logger.info(forwardInfo);
						//System.out.println(forwardInfo);
						weibo.setEventName(currentEvent.getEventname());
						//下载此微博的转发信息
						List<UserWeiboAction> actionList = wd.downloadUserWeiboAction(weibo, ActionType.FORWARD);
						//下载参与此事件的用户信息
						for(UserWeiboAction uwa : actionList)
						{
							dateValue = df.format(new Date());
							forwardInfo = String.format("[%s] 正在下载事件 [%s] 的用户 [%s] 的个人信息", dateValue, currentEvent.getEventname(), weibo.getUid());
							logger.info(forwardInfo);
							userDao.insertUser(wd.downloadUserInfo(uwa.getUid()));
						}
						//将转发信息写入数据库中
						uwaDao.insertActionList(actionList);
					}
					
//					//检查帐号是否正常
//					if(!wd.checkAccount())
//						wd = UserWeiboDownloadThread.getUsefulDownloader(downloaderList);
					
					
					
				}
				//若发生帐号异常，则切换帐号重新下载
				catch (AccountForbiddonException e)
				{
//					String oldAccount = wd.getLoginUser().getUserName();
//					wd.finalise();
//					//downloaderList.add(wd);
//					wd = UserWeiboDownloadThread.getUsefulDownloader(downloaderList);
//					String newAccount = wd.getLoginUser().getUserName();
//					System.out.println(String.format("帐号%s已经被封，切换为帐号%s,重新下载事件%s", oldAccount, newAccount, currentEvent.getEventname()));
					e.printStackTrace();
					continue;
				}
				catch (Exception e)
				{
//					String oldAccount = wd.getLoginUser().getUserName();
//					wd.finalise();
//					//downloaderList.add(wd);
//					wd = UserWeiboDownloadThread.getUsefulDownloader(downloaderList);
//					String newAccount = wd.getLoginUser().getUserName();
//					System.out.println(String.format("帐号%s已经被封，切换为帐号%s,重新下载事件%s", oldAccount, newAccount, currentEvent.getEventname()));
					e.printStackTrace();

					i--;
					
					continue;
					
				}
				
			}
			
			
		}
		
	//}
	
	
}
